﻿#include "ieCommon.h"

// -------------------------------------------------------------------------------------------------------------
size_t strlengthght( const char *string ) 
{
	const char *s;

	s = string;

	while ( *s ) 
	{
		s++;
	}

	return ( s - string );
}
// -------------------------------------------------------------------------------------------------------------
void strreplace( char *string, char oldchar, char newchar )
{
    unsigned int i = 0;
	
	while( *string )
	{
	    if( *string == oldchar ) 
		{
		    *string = newchar;
		}
		string++;
	}
}
// -------------------------------------------------------------------------------------------------------------
void strcopy( char *destin, char *source, size_t maxlengthgth )
{
    while( --maxlengthgth && *source )
	{
	    *destin = *source;
		destin++;
		source++;
	}
	*destin = 0;
}
// -------------------------------------------------------------------------------------------------------------
int strcompare( const char *string1, const char *string2 )
{
	int		c1, c2;

	do 
	{
		c1 = *string1++;
		c2 = *string2++;

		if( c1 != c2 )
		{
			if( c1 < c2 )
			{
				return -1; // string 1 is smaller than string2
			}
			else
			{
				return 1; // string 1 is bigger than string2
			}
		}
	} while( c1 );

	return 0; // Equal
}
// -------------------------------------------------------------------------------------------------------------
int strcomparechars( const char *string1, const char *string2, int n )
{
	int		c1, c2;

	do 
	{
		c1 = *string1++;
		c2 = *string2++;

		if( !n-- )
		{
			return 0;	// Equal
        }
		
		if( c1 != c2 )
		{
			if( c1 < c2 )
			{
				return -1; // string 1 is smaller than string2
			}	
			else
			{
				return 1; // string 1 is bigger than string2
			}	
		}
	} while( c1 );

	return 0;	// Equal
}
// -------------------------------------------------------------------------------------------------------------
int strtoint( const char *string )
{
	return ( atoi( string ) );
}
// -------------------------------------------------------------------------------------------------------------
float strtofloat( const char *string )
{
	return ( atof( string ) );
}
// -------------------------------------------------------------------------------------------------------------
const char *strvarargs( const char *buffer, ... )
{
	static char	string[8][MAX_STRING_LENGTH];
	static int	index;
	va_list		argPtr;
	char		*destin;
	int			maxlengthgth;
	int			length;

	destin = string[index];
	maxlengthgth = sizeof( string[index] );
	index = ( index + 1 ) & 7;

	va_start( argPtr, buffer );
	length = vsnprintf( destin, maxlengthgth, buffer, argPtr );
	va_end( argPtr );

	destin[maxlengthgth-1] = 0;

	if ( length < 0 || length >= maxlengthgth )
	{
		return "";
	}

	return destin;
}
// -------------------------------------------------------------------------------------------------------------
char *strfindchar( const char *string, int c )
{
	while ( *string )
	{
		if( *string == c )
		{
			return (char *)string;
		}

		string++;
	}

	return NULL;
}
// -------------------------------------------------------------------------------------------------------------
char *strfindcharrev( const char *string, int c )
{
	const char	*s;

	s = string + strlengthght( string );

	while ( s >= string )
	{
		if ( *s == c )
		{
			return (char *)s;
		}

		s--;
	}

	return NULL;
}
// -------------------------------------------------------------------------------------------------------------
int strsprintf( char *destin, int maxlengthgth, const char *buffer, ... )
{
	va_list	argPtr;
	int		length;

	va_start( argPtr, buffer );
	length = vsnprintf( destin, maxlengthgth, buffer, argPtr );
	va_end( argPtr );

	destin[maxlengthgth-1] = 0;

	if( length < 0 || length >= maxlengthgth )
	{
		return -1;
	}

	return length;
}
// -------------------------------------------------------------------------------------------------------------
int strvsprintf( char *destin, int maxlengthgth, const char *buffer, va_list argPtr )
{
	int		length;

	length = vsnprintf( destin, maxlengthgth, buffer, argPtr );

	destin[maxlengthgth-1] = 0;

	if( length < 0 || length >= maxlengthgth )
	{
		return -1;
	}

	return length;
}
// -------------------------------------------------------------------------------------------------------------
const char *strfromfloat( float value, int precision )
{
	static char	string[8][64];
	static int	index;
	char		buffer[16];
	char		*destin;
	int			maxLength;
	int			i;

	destin = string[index];
	maxLength = sizeof( string[index] );
	index = ( index + 1 ) & 7;

	if( precision > 0 )
	{
		snprintf( buffer, sizeof(buffer), "%%.%if", precision );

		snprintf( destin, maxLength, buffer, value );
	}
	else 
	{
		if( precision == 0 )
		{
			snprintf( destin, maxLength, "%i", (int)value );
		}
		else
		{
			snprintf( destin, maxLength, "%g", value );
		}
	}

	if( precision == -1 )
	{
		for( i = 0; destin[i]; i++ )
		{
			if( destin[i] == '.' )
			{
				break;
			}
		}

		if( !destin[i] && i < maxLength - 2 )
		{
			destin[i++] = '.';
			destin[i++] = '0';
			destin[i] = 0;
		}
	}

	destin[maxLength-1] = 0;

	return destin;
}
// -------------------------------------------------------------------------------------------------------------